// onload
$(document).ready(function() {
	sample.list();
});

var sample = {
	sampleUrl : contextPath + '/sample'
	// 데이터 목록 조회
	,list : function() {
		var request = $.ajax({
			url : this.sampleUrl
			,type : 'GET'
			,dataType : 'json'
		});
		request.done(function(list) {
			var html = [];
			html.push('<table>');
			html.push('<tr><th>순번</th><th>제목</th><th>작성자</th><th>작성일</th></tr>');
			var count = list.length;
			for(var i = 0; i < count; i++) {
				data = list[i];
				html.push('<tr>');
				html.push('<td>' + data['seq'] + '</td>');
				html.push('<td><a href="#view" onclick="sample.view(' + data['seq'] + '); return false;">');
				html.push(data['title']);
				html.push('</a></td>');
				html.push('<td>' + data['name'] + '</td>');
				html.push('<td>' + data['regDate'] + '</td>');
				html.push('</tr>');
			}
			html.push('</table>');
			html.push('<input type="button" value="등록" onclick="sample.regForm()" />');
			$('#content_area').html(html.join(""));
		});
		request.fail(function(jqXHR, textStatus) {
			alert('Request failed: ' + textStatus);
		});
	}
	// 데이터 상세보기 조회
	,view : function(seq) {
		var request = $.ajax({
			url : this.sampleUrl + '/' + seq
			,type : 'GET'
			,dataType : 'json'
		});
		request.done(function(data) {
			var html = [];
			html.push('<table>');
			html.push('<tr><th>순번</th><td>' + data['seq'] + '</td></tr>');
			html.push('<tr><th>제목</th><td>' + data['title'] + '</td></tr>');
			html.push('<tr><th>작성자</th><td>' + data['name'] + '</td></tr>');
			html.push('<tr><th>작성일</th><td>' + data['regDate'] + '</td></tr>');
			html.push('<tr><th>내용</th><td>' + data['content'].replace(/\n/g, '<br />') + '</td></tr>');
			html.push('</table>');
			html.push('<input type="button" value="삭제" onclick="sample.del(' + data['seq'] + ')" />');
			html.push('<input type="button" value="목록" onclick="sample.list()" />');
			$('#content_area').html(html.join(""));
		});
		request.fail(function(jqXHR, textStatus) {
			alert('Request failed: ' + textStatus);
		});
	}
	// 등록 폼 생성
	,regForm : function() {
		var html = [];
		html.push('<form id="regForm" method="POST" onsubmit="sample.reg(); return false;">');
		html.push('<table>');
		html.push('<tr><th>제목</th><td><input type="text" id="title" name="title" value="" style="width:600px;" /></td></tr>');
		html.push('<tr><th>작성자</th><td><input type="text" id="name" name="name" value="" style="width:600px;" /></td></tr>');
		html.push('<tr><th>내용</th><td><textarea id="content" name="content" style="width:600px; height:300px;"></textarea></td></tr>');
		html.push('</table>');
		html.push('<input type="submit" value="등록" />');
		html.push('<input type="button" value="목록" onclick="sample.list()" />');
		$('#content_area').html(html.join(""));
	}
	// 데이터 등록
	,reg : function() {
		if($.trim($('#title').val()) == '') {
			alert('제목을 입력하세요.');
			$('#title').focus();
			return;
		}
		// form 데이터 json 으로 변환
		var json = $.toJSON($('#regForm').serializeJSON());

		var request = $.ajax({
			url : this.sampleUrl
			,type : 'POST'
			,contentType : 'application/json; charset=UTF-8'
			,data : json
			,dataType : 'json'
		});
		request.done(function(data) {
			sample.list();
		});
		request.fail(function(jqXHR, textStatus) {
			alert('Request failed: ' + textStatus);
		});
	}
	// 데이터 삭제
	,del : function(seq) {
		if(confirm('삭제하시겠습니까?')) {
			var request = $.ajax({
				url : this.sampleUrl + '/' + seq
				,type : 'DELETE'
				,dataType : 'json'
			});
			request.done(function(data) {
				sample.list();
			});
			request.fail(function(jqXHR, textStatus) {
				alert('Request failed: ' + textStatus);
			});
		}
	}
};